package com.app.xinfadi.widget.dialog;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.base.BaseDialog;
import com.app.xinfadi.R;

import java.io.File;
import java.lang.ref.WeakReference;


/**
 * Function:
 * <br/> 下载进度条dialog
 * Create on:2015/10/22 19:21
 * <br/>
 * Author:WuTengFei
 */
public class DownDialog extends BaseDialog {
    private View contentView;
    private TextView titletTv;
    private String title;

    private ProgressBar mProgress;
    private TextView versionUpdatePrecentTv;
    private TextView cancelBtn;
    protected OnBtnClickL onBtnCancelClick;
    private String saveFileName;
    private int progress;
    private static final int DOWN_UPDATE = 1;
    private static final int DOWN_OVER = 2;

    /**
     * method execute order:
     * show:constrouctor---show---oncreate---onStart---onAttachToWindow
     * dismiss:dismiss---onDetachedFromWindow---onStop
     *
     * @param context
     */
    public DownDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        contentView = View.inflate(mContext, R.layout.version_update_progress, null);
        titletTv = (TextView) contentView.findViewById(R.id.titletTv);
        mProgress = (ProgressBar) contentView.findViewById(R.id.versionUpdatePb);
        versionUpdatePrecentTv = (TextView) contentView.findViewById(R.id.versionUpdatePrecentTv);
        cancelBtn = (TextView) contentView.findViewById(R.id.cancelBtn);
        return contentView;
    }

    @Override
    public void setUiBeforShow() {
        setCanceledOnTouchOutside(false);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBtnCancelClick != null)
                    onBtnCancelClick.onBtnClick();
            }
        });
    }

    public static class DownDialogHandler extends Handler {
        private final WeakReference<DownDialog> outerClass;

        public DownDialogHandler(DownDialog innerClass) {
            outerClass = new WeakReference<DownDialog>(innerClass);
        }

        @Override
        public void handleMessage(Message msg) {
            DownDialog innerClass = outerClass.get();
            switch (msg.what) {
                case DOWN_UPDATE:
                    int progress1 = msg.arg1;
                    innerClass.mProgress.setProgress(progress1);
                    innerClass.versionUpdatePrecentTv.setText(progress1 + "%");
                    break;
                case DOWN_OVER:
                    if (innerClass != null) {
                        innerClass.dismiss();
                        innerClass.installApk();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    public DownDialog setSaveFileNamel(String saveFileName) {
        this.saveFileName = saveFileName;
        return this;
    }

    /**
     * 安装APK
     */
    private void installApk() {
        File apkfile = new File(saveFileName);
        if (!apkfile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
        mContext.startActivity(i);
    }

    public DownDialog setOnBtnCancelClick(OnBtnClickL onBtnCancelClick) {
        this.onBtnCancelClick = onBtnCancelClick;
        return this;
    }

    public DownDialog setTitle(String title) {
        this.title = title;
        return this;
    }
}
